﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BLL;
using DAL;

namespace QLTTTH
{
    public partial class frmThongTinHocPhi : DevExpress.XtraEditors.XtraForm
    {
        public frmThongTinHocPhi()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        public void FillControl(BienLai hocphi)
        {
            this.txtMaBL.Text = hocphi.MaBienLai.ToString();
            this.txtMaDK.Text = hocphi.MaDangKy.ToString();
            this.loupHocVien.EditValue = hocphi.MaHV;
            this.loupNhanVien.EditValue = hocphi.MaNV;
            this.dtNgaythu.EditValue = hocphi.NgayThu.ToShortDateString();
            this.txtTienHP.Text = hocphi.TienHP.ToString();
        }



        public delegate void SendData(BienLai hocphi);

        public SendData sendData;

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (sendData != null)
            {
                var vResult = new HocPhiManager().Validate(LayBienLai());
                if (vResult.Valid)
                {
                    sendData(LayBienLai());
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    XtraMessageBox.Show(vResult.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        public BienLai LayBienLai()
        {
            BienLai hocphi = new BienLai();

            if (!txtMaBL.Text.ToString().Equals(""))
            {
                hocphi.MaBienLai = int.Parse(txtMaBL.Text.ToString());
            }

            hocphi.MaDangKy = int.Parse(txtMaDK.Text.ToString());
            hocphi.MaHV = int.Parse(loupHocVien.EditValue.ToString());
            hocphi.MaNV = int.Parse(loupNhanVien.EditValue.ToString());
            hocphi.TienHP = decimal.Parse(txtTienHP.Text.ToString());
            hocphi.NgayThu = DateTime.Parse(dtNgaythu.Text);
            return hocphi;
        }

        private void frmThongTinHocPhi_Load(object sender, EventArgs e)
        {
            var dsHV = HocVienManager.LayDanhSachHocVien();
            if (dsHV.Success)
            {
                loupHocVien.Properties.DataSource = dsHV.Result;
                loupHocVien.Properties.ValueMember = "MaHV";
                loupHocVien.Properties.DisplayMember = "HoTen";
            }
            var dsNV = NhanVienManager.TimDanhSachNhanVien("Kế toán",
                (object a, object b) =>
                {
                    return (b as NguoiDung).ChucVu.CompareTo(a);
                });
            if (dsNV.Success)
            {
                loupNhanVien.Properties.DataSource = dsNV.Result;
                loupNhanVien.Properties.ValueMember = "MaNV";
                loupNhanVien.Properties.DisplayMember = "HoTen";

            }

        }

    }
}